
package question_answer;
import java.util.ArrayList;
import jess.Rete;
import jess.Fact;
import jess.Value;
import jess.RU;


class Struct_Template{
    private String STR_NAME;
    private String STR_HEAD;
    private String STR_DEPENDENCE;
    public Struct_Template(String rStr) //Example nsbuj(Khue-1,eat-2).
    {
        int int_firstPosition = rStr.indexOf('(');
        STR_NAME = rStr.substring(0, int_firstPosition).trim();
        int int_secondPostion = rStr.indexOf('-');
        STR_HEAD = rStr.substring(int_firstPosition+1, int_secondPostion).trim();
        int int_thirdPosition = rStr.indexOf(',');
        int int_fourthPosition = rStr.indexOf('-',int_thirdPosition);
        STR_DEPENDENCE = rStr.substring(int_thirdPosition+1,int_fourthPosition).trim();
    }
    public String getName()
    {
        return this.STR_NAME;
    }
    public String getHead()
    {
        return this.STR_HEAD;
    }
    public String getDependence()
    {
        return this.STR_DEPENDENCE;
    }
  
}
public class AddWorkingMemory {
    ArrayList<Struct_Template> LIST_STATEMENT;
    ArrayList<Struct_Template> LIST_QUESTION;
    //Deffacts FACT_STATEMENT;
    //Deffacts FACT_QUESTION;
    public AddWorkingMemory()
    {
        this.LIST_STATEMENT = new ArrayList<Struct_Template>();
        this.LIST_QUESTION = new ArrayList<Struct_Template>();

        //this.FACT_STATEMENT = new Deffacts("Statement","Statement sentence",rRete);
    }

    public void AddFact(Rete rRETE)
    {
        try{
            for(Struct_Template template: this.LIST_STATEMENT)
            {
                Fact fact = new Fact(template.getName(), rRETE);
                fact.setSlotValue("Head", new Value(template.getHead(),RU.STRING));
                fact.setSlotValue("Dependence", new Value(template.getDependence(),RU.STRING));
                fact.setSlotValue("Pattern", new Value(1,RU.INTEGER));
                rRETE.assertFact(fact);
            }
            for(Struct_Template template: this.LIST_QUESTION)
            {
                Fact fact = new Fact(template.getName(), rRETE);
                fact.setSlotValue("Head", new Value(template.getHead(),RU.STRING));
                fact.setSlotValue("Dependence", new Value(template.getDependence(),RU.STRING));
                fact.setSlotValue("Pattern", new Value(0,RU.INTEGER));
                rRETE.assertFact(fact);
            }
        }
        catch(Exception e)
        {
            System.out.println("Error at AddFact in AddWorkingMemory\n" + e.toString());
        }
        
    }

    void CreateListTemplate(String rStr, int rPa)// rsentence la loai cau: 0: cau hoi,1 cau khang dinh.
   {
        try{
        if(rPa == 1)
        {
            this.LIST_STATEMENT.clear();
            int endofString = rStr.length();
            int index_next = rStr.indexOf(")");
            int index = 1;
            while(true)
            {
                String strTemp = rStr.substring(index, index_next+1).trim();
                this.LIST_STATEMENT.add(new Struct_Template(strTemp));
                index = index_next+2;
                if(index <endofString)
                    index_next = rStr.indexOf(")",index+1);
                else
                    break;
            }
        }
        else
        {
            this.LIST_QUESTION.clear();
            int endofString = rStr.length();
            int index_next = rStr.indexOf(")");
            int index = 1;
            while(true)
            {
                String strTemp = rStr.substring(index, index_next+1).trim();
                this.LIST_QUESTION.add(new Struct_Template(strTemp));
                index = index_next+2;
                if(index <endofString)
                    index_next = rStr.indexOf(")",index+1);
                else
                    break;
            }
        }
       }
        catch(Exception e)
        {
           System.out.println("Error CreateListTemplate" + e.toString());
        }
    }
    public ArrayList<Struct_Template>  getStatementFact()
    {
        return this.LIST_STATEMENT;
    }

    public ArrayList<Struct_Template>  getQuestionFact()
    {
        return this.LIST_QUESTION;
    }
}
